package com.nmcc.app_adherence;

import java.io.File;
import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class ResendBluetoothFile extends Activity {


	private static String CSV_NAME = "GPSData.csv";
	private static String CSV_NAME_PERSONS = "Persons.csv";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_resend_bluetooth_file);
		
		 
		
	     Button b2 = (Button) findViewById(R.id.btnStart);
	        b2.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View arg0) {

	        	   ExportDataToCSVLinker();
	        	   SendFileViaBluetoothFromSDCardFolder(arg0);
	        
	           }
	        });  
		 
        Button b3 = (Button) findViewById(R.id.btnResendBluetoothFile);
        b3.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {

        	   SendFileViaBluetoothFromSDCardFolder(arg0);
        
           }
        });  
        
        //Button b4 = (Button) findViewById(R.id.btnClose);
       // b4.setOnClickListener(new View.OnClickListener() {
          // public void onClick(View arg0) {

        	   //CloseThisActivity();
        
           //}
       // });  
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.resend_bluetooth_file, menu);
		return true;
	}

	
	public void CloseThisActivity(){
		 final Context context = this;
	     //give values
		   Intent intent = new Intent(context, ResendBluetoothFile.class);
	     finish();

	     
	}
	
	public void ExportDataToCSVLinker(){
		
		  ExportGPSDataToCSV();
  	    //Toast.makeText(getApplicationContext(), "Sending GPSData, Persons csv files via bluetooth.", Toast.LENGTH_LONG).show();
 	  // SendFileViaBluetoothFromSDCardFolder();
	}
	
	   
	   public void SendFileViaBluetoothFromSDCardFolder(View view){
		  
		
		   
		   File sdcard = Environment.getExternalStorageDirectory();
			   File f1=new File(sdcard+"/GPSData.csv");
			   File f2=new File(sdcard+"/Persons.csv");

			   Uri csvUri1 = Uri.fromFile(f1);
			   Uri csvUri2 = Uri.fromFile(f2);
			   
			   ArrayList<Uri> csvUris = new ArrayList<Uri>();
			   csvUris.add(csvUri1); // Add your image URIs here
			   csvUris.add(csvUri2);	
		   
			   
		 //...
		   // inside method
		   // Check if bluetooth is supported
		   BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
		   if (btAdapter == null) {
		   // Device does not support Bluetooth
		   // Inform user that we're done.     
		   }
		   // bring up Android chooser
		   Intent intent = new Intent();
		   intent.setAction(Intent.ACTION_SEND_MULTIPLE);
		   
		   //intent.setType("application/csv");
		   //send file 1
		   //intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
		   intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, csvUris);
		   
		   //remove below to have flexibilty on the bluetooth complete action
		   //intent.setPackage("com.mediatek.bluetooth");//"com.android.bluetooth"
		   //can remove above line, or make it as a setting editable
		   
		   //...
		   //startActivity(intent);
		   //send file 2

		   intent.setType("application/csv");
		   startActivity(Intent.createChooser(intent, "Send GPSData and Persons Files Via:"));
		  //startActivity(intent);
		

		    
		} 

	   public void ExportGPSDataToCSV(){
	  	   MySQLiteHelper db = new MySQLiteHelper(this);
	  	   //to avoid overwriting in odk survey just send to sd card folder
	         // add Books
	         //db.DropAllTables();
	       db.ExportGPSDataToCSV(CSV_NAME);
	        //db.getGPSRecordsCount();
	       db.ExportPersonsToCSV(CSV_NAME_PERSONS);
	      // Toast.makeText(getApplicationContext(), "Files successfully exported to csv. Please check this data in the ODK Survey", 5000).show();
	         //new
	  	   //now send via bluetooth
	  	   //Toast.makeText(getApplicationContext(), "Now Sending GPSData and Persons csv files via bluetooth.", 5000).show();
	         
	  	   //SendFileViaBluetoothFromSDCardFolder(view, "GPSData.csv");
	  	   //SendFileViaBluetoothFromSDCardFolder(view, "Persons.csv");
	  }
	
}
